package org.cttic.visualization.visualization.util.sevenUtil;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cttic.visualization.visualization.common.module.roadEventData;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.GpsCoordinateUtils;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.websocket.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-06-30 11:01
 */
@Component
public class RoadEventsUtil {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    public void roadEventQuery(String value){
        /*
        {
            "vehiclePlate": "晋C62992",
            "vehicleColor": 2,
            "transTime": "2020-06-30 11:08:11",
            "pointType": 1,
            "gantryId": "G002014041001110010",
            "gantryName": "离石东至离石西1",
            "enExFlag": null,
            "longitude": 111.174965,
            "latitude": 37.523411,
            "serverTime": "2020-06-30 11:23:10",
            "sourceSupport": "1",
            "speed": 69
        }
         */
        JSONObject data = ParseJsonDataUtil.getJsonData(value);
        int pointType = data.getIntValue("pointType");
        if(pointType==3){

//        String vehicleId = data.getString("vehiclePlate");
//        int vehicleIdColor = data.getIntValue("vehicleColor");
        String transTime = data.getString("transTime");
        String gantryId = data.getString("gantryId");
        String gantryName = data.getString("gantryName");
        String latitude = data.getString("latitude");
        String longitude = data.getString("longitude");
        double lat = GpsCoordinateUtils.calWGS84toGCJ02Lat(Double.parseDouble(latitude), Double.parseDouble(longitude));
        double log = GpsCoordinateUtils.calWGS84toGCJ02Log(Double.parseDouble(latitude), Double.parseDouble(longitude));
//        String endLat = data.getString("endLat");
        String enExFlag = data.getString("enExFlag");
        String speed = data.getString("speed");
        roadEventData roadEventData = new roadEventData();

        roadEventData.setCmd("188");
        roadEventData.setProvinceId(getProvinceId(gantryId));
        roadEventData.setPointType(getPointType(pointType+""));
        roadEventData.setGantryId(gantryId);
        roadEventData.setGantryName(gantryName);
        roadEventData.setLat(lat+"");
        roadEventData.setLng(log+"");
        if(enExFlag!=null){
            roadEventData.setDirection(enExFlag);
        }else {
            roadEventData.setDirection(getDirection());
        }
        roadEventData.setReportTime(DateUtil.timeToT(transTime));
        roadEventData.setTrafficLevel(getTrafficLevel(speed));
            ObjectMapper mapper = new ObjectMapper();
            try {
                String roadData = mapper.writeValueAsString(roadEventData);
                Data.sendMessage(JSONUtil.parseObj(roadData));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            System.out.println("--------发送收费站路况信息："+roadEventData.toString()+"----------");
        }
    }
    private String getTrafficLevel(String speed){
        if(Integer.parseInt(speed)>22){
            return "5";
        }else  if(Integer.parseInt(speed)>15){
            return "4";
        }else if (Integer.parseInt(speed)>8){
            return "3";
        }else if (Integer.parseInt(speed)>5){
            return "2";
        }else {
            return "1";
        }
    }
    private String getDirection(){
        double random = Math.random();
        if(random>0.5){
            return "1";
        }else {
            return "2";
        }
    }
    private String getProvinceId(String gantryId){
        String stationSql ="SELECT stationId,provinceId FROM `t_station` WHERE stationId='"+gantryId+"'";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(stationSql);
        if(maps!=null&&maps.size()>0){
            Map<String, Object> stringObjectMap = maps.get(0);
            String provinceId = stringObjectMap.get("provinceId").toString();
            return provinceId;
        }
        return null;
    }
    public String getPointType(String input){
        if(input.equals("1")){
            return "3";
        }else if(input.equals("2")){
            return "1";
        }else if(input.equals("3")){
            return "2";
        }else{
            return null;
        }
    }
}
